package action

import (
	"core/action"
	"core/auth"
	"core/coredto"
	"core/corestruct"
	"core/coreutil"
	"core/myerrors"
	"core/server/httpserver"
	"ht/selfreg"
	"reflect"
	"strings"
)

type LoginAction struct {
}

func (p *LoginAction) Do(mm *coredto.LoginDto, ctx *corestruct.HttpContext) (string, error) {
	if httpserver.Config.Mode != "debug" {
		if "" == mm.YzmId {
			return "", myerrors.New("验证码错误")
		}

		if "" == mm.YzmId {
			return "", myerrors.New("验证码错误")
		}
		var yzm, err = coreutil.RedisUtil.Get(YZMPrev + mm.YzmId).Result()
		if err != nil {
			return "", myerrors.NewByCode(myerrors.ERROR_UNKNOW, err.Error())
		}
		yzm = strings.ToLower(mm.YzmValue)
		if yzm != mm.YzmValue {
			return "", myerrors.New("验证码错误")
		}
	}
	token, myerr := auth.Login(mm, ctx)
	if myerr != nil {
		return "", myerr
	}
	return token, nil
}

func init() {

	var mact = &LoginAction{}
	var maction = action.Action{
		Method: "get",

		FullUrl: "/htlogin/login",
		Name:    "登录",
		Ename:   "",
		Memo:    "",

		PinType:    "",
		PinIslist:  false,
		PoutType:   "",
		PoutIslist: false,
		PoutIspage: false,

		AuthType: action.AuthType_NONE,

		LogNeed:     false,
		LogPin:      false,
		LogPout:     false,
		LogTemplete: "",

		PrjName: "",
		Value:   reflect.ValueOf(mact),
	}
	selfreg.AddAction(&maction)

}
